class GwtController<ApplicationController
  def agent
    @agents = WWW::Tkd::Agent.find(:all)
    render :text => @agents.to_json
  end
  def index
    user_id = session[:id]
    start = params[:start]
    limit = params[:limit]
    @agents = WWW::Tkd::Agent.find(:all)
    orders = WWW::Tkd::TbBuyerOrder.find_on_page(user_id,start.to_i,limit.to_i)
    @tb_orders=Array.new
    orders.each do|item|
      @tb_orders.push(WWW::Tkd::GwtOrder.init_by_order(item))
    end
    result = Hash.new
    result['offset']=start.to_i
    result['count']=WWW::Tkd::TbBuyerOrder.count user_id
    result['items']=@tb_orders
    render :text => result.to_json
  end
  def view

  end
  def saveno
    user_id = session[:id]
    agent_id = params[:agent_id]
    no_code=params[:no_code]
    order_id=params[:order_id]
    agent = WWW::Tkd::Agent.find_by_id agent_id
    result = Hash.new
    if agent==nil
      result[:flag]=-1
      result[:msg]="快递公司不存在"
      render :text => result.to_json
      return
    end
    #组装TbOrder对象
    order = WWW::Tkd::TbOrder.find_from_id_and_user_id(order_id,user_id)
    if(order==nil)
      result[:flag]=-2
      result[:msg]="交易订单不存在"
      render :text => result.to_json
      return
    end
    #组装Express对象
    express=WWW::Tkd::Express.new
    express.agent_id=agent_id
    express.no_code=no_code
    #分解对象并进行保存
    WWW::Tkd::GwtOrder.save order,express
    result[:flag]=1
    result[:msg]="success"
    render :text => result.to_json
  end
end
